﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Data;
using System.Collections.Generic;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class FaqEdit : VITComponent
    {
        VIT.Provider.Faq obj;
        public string genOption = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            Language language = new Language();
            obj = new VIT.Provider.Faq();

            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }

            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                loadData();
                loadLanguages(language, obj.LanguageId);                
            }
        }

        #region Methods
        private void loadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();

            if (obj.Id > 0)
            {
                txtQuestion.Text = obj.Question;
                txtAskBy.Text = obj.AskBy;
                //txtAnswer.Text = obj.Answer;
                Contents.Value = obj.Answer;
                txtAnswerBy.Text = obj.AnswerBy;
                txtEmail.Text = obj.Email;
                txtAddress.Text = obj.Address;
                txtOrder.Text = obj.Orders.ToString();
                chkPublish.Checked = obj.Published;
            }
            LoadParent();
        }

        private void loadLanguages(Language language, string selectedId)
        {
            ddlLang.DataSource = language.getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            if (string.IsNullOrEmpty(selectedId)) ddlLang.SelectedValue = Session["language"].ToString();
            else ddlLang.SelectedValue = selectedId;
        }

        private void LoadParent()
        {
            VIT.Provider.FaqGroup f = new VIT.Provider.FaqGroup();
            f.LanguageId = Session["language"].ToString();
            this.genOption = f.GetAll().GenTagOption("Id", "Title", "0", obj.FaqGroupId.ToString());
        }

        private void save()
        {
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtOrder, new List<object> {"number"}}
                                                    };
            if (MainCore.checkData(data))
            {
                obj.LanguageId = ddlLang.SelectedValue;
                obj.Question = txtQuestion.Text;
                obj.AskBy = txtAskBy.Text;
                obj.Answer = Contents.Value;
                obj.FaqGroupId = int.Parse(Request["parentid"]);
                obj.AnswerBy = txtAnswerBy.Text;
                obj.Email = txtEmail.Text;
                obj.Address = txtAddress.Text;
                obj.Orders = int.Parse(txtOrder.Text);
                obj.Published = chkPublish.Checked;

                if (obj.Save() > 0) setTrueVisible(lblThanhCong);
                else setTrueVisible(lblThatBai);
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Exchange", "FaqList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Exchange", "FaqList");
        }
        #endregion
    }
}
